Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always relate with Invariant to non-General inference vars #659

Merged
merged 3 commits into from
Dec 5, 2020

Conversation

jackh726
Copy link
Member

Fixes #658

An alternative way to approach this is to always unify inference variables as Invariant, which is how it's implemented in rustc right now. We should think about if we want that long term though (and possibly come up with tests that hit that).

@matthewjasper
Copy link
Contributor

Rustc doesn't unify inference variables with subtype bounds:

https://github.com/rust-lang/rust/blob/b776d1c3e3db8befabb123ebb1e46c3531eaed46/compiler/rustc_infer/src/infer/sub.rs#L87-L112

Looking at unification, the only difference here with rustc in this area is that {int_var} <: {ty_var} is turned into {int_var} = {ty_var} (and likewise with floats, and with the subtype relation reversed).

@jackh726
Copy link
Member Author

Looking at unification, the only difference here with rustc in this area is that {int_var} <: {ty_var} is turned into {int_var} = {ty_var} (and likewise with floats, and with the subtype relation reversed).

Ugh yeah, that's what I was missing and what the proper change is.

@jackh726
Copy link
Member Author

Actually, that alone is not enough to make the test pass :/

@matthewjasper
Copy link
Contributor

I'm not sure if the test case with two arbitrary variables needs to pass. One of the variables will eventually be resolved if the program is going to compile.

@jackh726
Copy link
Member Author

I wondered about that. I did test with the original test from @flodiebold with the int T and that didn't pass.

@nikomatsakis
Copy link
Contributor

I don't love this solution. Like @matthewjasper, I'm not sure that the test with two arbitrary variables needs to pass. @jackh726 can you share what you tried with integer variables? In rustc, we definitely consider integer variables "more specific" than normal variables (so e.g. a general variable would be "assigned" the value of an integer variable).

@jackh726
Copy link
Member Author

Yeah, I updated.

@jackh726
Copy link
Member Author

So, to elaborate on why only always unifying non-general inference vars doesn't work:
First, we start off trying to unify our goal Implemented(MyClosure<"rust" for<0> [?0 := (&'static ?0), ?1 := 0]>: FnOnce<1<(&'static ?1i)>>) with our (instantiated) clause Implemented(MyClosure<"rust" for<0> [?0 := ?2, ?1 := 0]>: FnOnce<1<?2>>).

From there, we try to unify MyClosure<"rust" for<0> [?0 := (&'static ?0), ?1 := 0]> and MyClosure<"rust" for<0> [?0 := ?2, ?1 := 0]> first. When ?2 tries to get unified with (&'static ?0), it is generalized and set to (&'?3 ?4). From the next unification, we get ?4 < ?0 (and the reverse too, since both Covariant and Contravariant, since we're in Invariant).

By this point, when we unify 1<(&'static ?1i)> and 1<?2>, it's too late, and we've already added the subtype goals.

@jackh726
Copy link
Member Author

jackh726 commented Dec 1, 2020

Okay, so we probably just need to actually resolve inference vars when trying to simplify a subtype goal...

@jackh726
Copy link
Member Author

jackh726 commented Dec 1, 2020

Okay so that worked.

@jackh726 jackh726 changed the title Don't create unification goals when we should relate with Invariant Always relate with Invariant to non-General inference vars Dec 2, 2020
@nikomatsakis nikomatsakis merged commit 55b204d into rust-lang:master Dec 5, 2020
@nikomatsakis
Copy link
Contributor

oops, I forgot, we use bors now don't we... d'oh

@jackh726 jackh726 deleted the issue-658 branch December 5, 2020 07:21
@jackh726 jackh726 restored the issue-658 branch December 15, 2020 19:40
@jackh726 jackh726 deleted the issue-658 branch January 5, 2021 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression with unification within fn (?)
3 participants